use crate::src::test::alloc_testing::{alloc_test_realloc, alloc_test_strdup, alloc_test_get_allocated, alloc_test_calloc, alloc_test_set_limit, alloc_test_malloc, alloc_test_free};
use crate::src::test::framework::{run_tests};
use crate::src::struct_and_type::{size_t, UnitTestFunction};
use ::libc;
extern "C" {
    fn strcmp(_: *const libc::c_char, _: *const libc::c_char) -> libc::c_int;
    
    
    
    
    
    
    
    
}
unsafe extern "C" fn test_malloc_free() {
    let mut block: *mut libc::c_void = 0 as *mut libc::c_void;
    let mut block2: *mut libc::c_void = 0 as *mut libc::c_void;
    let mut block3: *mut libc::c_void = 0 as *mut libc::c_void;
    let mut block4: *mut libc::c_void = 0 as *mut libc::c_void;
    let mut ptr: *mut libc::c_uchar = 0 as *mut libc::c_uchar;
    let mut i: libc::c_int = 0;
    assert!(alloc_test_get_allocated() == 0 as libc::c_int as libc::c_ulong);
    block = alloc_test_malloc(1024 as libc::c_int as size_t);
    assert!(!block.is_null());
    assert!(alloc_test_get_allocated() == 1024 as libc::c_int as libc::c_ulong);
    ptr = block as *mut libc::c_uchar;
    i = 0 as libc::c_int;
    while i < 1024 as libc::c_int {
        assert!(*ptr.offset(i as isize) as libc::c_int != 0 as libc::c_int);
        i += 1;
        i;
    }
    alloc_test_free(block);
    assert!(alloc_test_get_allocated() == 0 as libc::c_int as libc::c_ulong);
    alloc_test_set_limit(3 as libc::c_int);
    block = alloc_test_malloc(1024 as libc::c_int as size_t);
    assert!(!block.is_null());
    block2 = alloc_test_malloc(1024 as libc::c_int as size_t);
    assert!(!block2.is_null());
    block3 = alloc_test_malloc(1024 as libc::c_int as size_t);
    assert!(!block3.is_null());
    block4 = alloc_test_malloc(1024 as libc::c_int as size_t);
    assert!(block4.is_null());
    alloc_test_free(block);
    alloc_test_free(block2);
    alloc_test_free(block3);
    alloc_test_free(block4);
}
unsafe extern "C" fn test_realloc() {
    let mut block: *mut libc::c_void = 0 as *mut libc::c_void;
    let mut block2: *mut libc::c_void = 0 as *mut libc::c_void;
    block2 = alloc_test_malloc(1024 as libc::c_int as size_t);
    block = alloc_test_malloc(1024 as libc::c_int as size_t);
    assert!(!block.is_null());
    assert!(alloc_test_get_allocated()
        == (1024 as libc::c_int + 1024 as libc::c_int) as libc::c_ulong);
    block = alloc_test_realloc(block, 2048 as libc::c_int as size_t);
    assert!(!block.is_null());
    assert!(alloc_test_get_allocated()
        == (2048 as libc::c_int + 1024 as libc::c_int) as libc::c_ulong);
    block = alloc_test_realloc(block, 1500 as libc::c_int as size_t);
    assert!(!block.is_null());
    assert!(alloc_test_get_allocated()
        == (1500 as libc::c_int + 1024 as libc::c_int) as libc::c_ulong);
    alloc_test_free(block);
    assert!(alloc_test_get_allocated()
        == (0 as libc::c_int + 1024 as libc::c_int) as libc::c_ulong);
    block = alloc_test_realloc(0 as *mut libc::c_void, 1024 as libc::c_int as size_t);
    assert!(!block.is_null());
    assert!(alloc_test_get_allocated()
        == (1024 as libc::c_int + 1024 as libc::c_int) as libc::c_ulong);
    alloc_test_free(block);
    alloc_test_free(block2);
    assert!(alloc_test_get_allocated() == 0 as libc::c_int as libc::c_ulong);
    block = alloc_test_malloc(512 as libc::c_int as size_t);
    assert!(!block.is_null());
    assert!(alloc_test_get_allocated() == 512 as libc::c_int as libc::c_ulong);
    alloc_test_set_limit(1 as libc::c_int);
    block = alloc_test_realloc(block, 1024 as libc::c_int as size_t);
    assert!(!block.is_null());
    assert!(alloc_test_get_allocated() == 1024 as libc::c_int as libc::c_ulong);
    assert!((alloc_test_realloc(block, 2048 as libc::c_int as size_t)).is_null());
    assert!(alloc_test_get_allocated() == 1024 as libc::c_int as libc::c_ulong);
    alloc_test_free(block);
    assert!(alloc_test_get_allocated() == 0 as libc::c_int as libc::c_ulong);
    alloc_test_set_limit(1 as libc::c_int);
    block = alloc_test_realloc(0 as *mut libc::c_void, 1024 as libc::c_int as size_t);
    assert!(!block.is_null());
    assert!(alloc_test_get_allocated() == 1024 as libc::c_int as libc::c_ulong);
    assert!((alloc_test_realloc(0 as *mut libc::c_void, 1024 as libc::c_int as size_t))
        .is_null());
    assert!(alloc_test_get_allocated() == 1024 as libc::c_int as libc::c_ulong);
    alloc_test_free(block);
}
unsafe extern "C" fn test_calloc() {
    let mut block: *mut libc::c_uchar = 0 as *mut libc::c_uchar;
    let mut i: libc::c_int = 0;
    assert!(alloc_test_get_allocated() == 0 as libc::c_int as libc::c_ulong);
    block = alloc_test_calloc(16 as libc::c_int as size_t, 64 as libc::c_int as size_t)
        as *mut libc::c_uchar;
    assert!(alloc_test_get_allocated() == 1024 as libc::c_int as libc::c_ulong);
    assert!(!block.is_null());
    i = 0 as libc::c_int;
    while i < 1024 as libc::c_int {
        assert!(*block.offset(i as isize) as libc::c_int == 0 as libc::c_int);
        i += 1;
        i;
    }
    alloc_test_free(block as *mut libc::c_void);
    assert!(alloc_test_get_allocated() == 0 as libc::c_int as libc::c_ulong);
    alloc_test_set_limit(1 as libc::c_int);
    block = alloc_test_calloc(1024 as libc::c_int as size_t, 1 as libc::c_int as size_t)
        as *mut libc::c_uchar;
    assert!(!block.is_null());
    assert!(alloc_test_get_allocated() == 1024 as libc::c_int as libc::c_ulong);
    assert!((alloc_test_calloc(1024 as libc::c_int as size_t, 1 as libc::c_int as size_t))
        .is_null());
    assert!(alloc_test_get_allocated() == 1024 as libc::c_int as libc::c_ulong);
    alloc_test_free(block as *mut libc::c_void);
}
unsafe extern "C" fn test_strdup() {
    let mut str: *mut libc::c_char = 0 as *mut libc::c_char;
    assert!(alloc_test_get_allocated() == 0 as libc::c_int as libc::c_ulong);
    str = alloc_test_strdup(b"hello world\0" as *const u8 as *const libc::c_char);
    assert!(!str.is_null());
    assert!(strcmp(str, b"hello world\0" as *const u8 as *const libc::c_char)
        == 0 as libc::c_int);
    assert!(alloc_test_get_allocated() == 12 as libc::c_int as libc::c_ulong);
    alloc_test_free(str as *mut libc::c_void);
    assert!(alloc_test_get_allocated() == 0 as libc::c_int as libc::c_ulong);
    alloc_test_set_limit(1 as libc::c_int);
    str = alloc_test_strdup(b"hello world\0" as *const u8 as *const libc::c_char);
    assert!(!str.is_null());
    assert!(alloc_test_get_allocated() == 12 as libc::c_int as libc::c_ulong);
    assert!((alloc_test_strdup(b"hello world\0" as *const u8 as *const libc::c_char))
        .is_null());
    assert!(alloc_test_get_allocated() == 12 as libc::c_int as libc::c_ulong);
    alloc_test_free(str as *mut libc::c_void);
}
unsafe extern "C" fn test_limits() {
    let mut block: *mut libc::c_void = 0 as *mut libc::c_void;
    block = alloc_test_malloc(2048 as libc::c_int as size_t);
    assert!(!block.is_null());
    alloc_test_free(block);
    alloc_test_set_limit(1 as libc::c_int);
    block = alloc_test_malloc(1024 as libc::c_int as size_t);
    assert!(!block.is_null());
    assert!((alloc_test_malloc(1024 as libc::c_int as size_t)).is_null());
    alloc_test_free(block);
    alloc_test_set_limit(-(1 as libc::c_int));
    block = alloc_test_malloc(1024 as libc::c_int as size_t);
    assert!(!block.is_null());
    alloc_test_free(block);
}
static mut tests_test_alloc_testing: [UnitTestFunction; 6] = unsafe {
    [
        Some(test_malloc_free as unsafe extern "C" fn() -> ()),
        Some(test_realloc as unsafe extern "C" fn() -> ()),
        Some(test_calloc as unsafe extern "C" fn() -> ()),
        Some(test_strdup as unsafe extern "C" fn() -> ()),
        Some(test_limits as unsafe extern "C" fn() -> ()),
        None,
    ]
};
unsafe fn main_0_test_alloc_testing(
    mut argc: libc::c_int,
    mut argv: *mut *mut libc::c_char,
) -> libc::c_int {
    run_tests(tests_test_alloc_testing.as_mut_ptr());
    return 0 as libc::c_int;
}
pub fn main_test_alloc_testing() {
    let mut args: Vec::<*mut libc::c_char> = Vec::new();
    for arg in ::std::env::args() {
        args.push(
            (::std::ffi::CString::new(arg))
                .expect("Failed to convert argument into CString.")
                .into_raw(),
        );
    }
    args.push(::core::ptr::null_mut());
    unsafe {
        ::std::process::exit(
            main_0_test_alloc_testing(
                (args.len() - 1) as libc::c_int,
                args.as_mut_ptr() as *mut *mut libc::c_char,
            ) as i32,
        )
    }
}
